<?php
namespace Util;
/**
* validate
*/
final class Validate
{
    const EMAIL = '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/';
    const PHONE = '/^((010|02[0-9]|0[3-9][0-9]{2})-)?(\d){7,9}(-\d+)?$/';
    const MOBILE = '/^1[3|4|5|7|8]{1}\d{9}$/';
    const QQ = '/^[1-9]([0-9]{5,11})$/';
    const ACCOUNT = '/^[a-zA-Z0-9_]{6,20}$/';

    /**
     * test 验证方法
     * @param  $key
     * @param  $str
     * @return BOOL
     * @date   2016-11-08
     */
    public static function test($key, $str)
    {
        $key = strtoupper((string)$key);
        $const = "self::$key";
        if(empty($key) || !defined($const)) {
            return false;
        }

        return preg_match(constant($const), $str) ? true : false;
    }

    /**
     * 获取单个正则
     * @param  string     $key
     * @return string
     * @author imjcw
     * @date   2016-11-14
     */
    public static function getPattern($key)
    {
        $key = strtoupper((string)$key);
        $const = "self::$key";
        if (!defined($const)) {
            return false;
        }
        return constant($const);
    }

    /**
     * 获取所有定义的正则
     * @return array
     * @date   2016-11-08
     */
    public static function getPatterns()
    {
        $oClass = new \ReflectionClass(__CLASS__);
        return $oClass->getConstants();
    }

    private function subImg($img)
    {
        require_once ROOT . 'lib/alioss/sdk.class.php';
        require_once ROOT . 'lib/alioss/util/oss_util.class.php';
        $file_name = $this->guid() . '.jpg';
        $oss = new \ALIOSS(OSS_ACCESS_ID, OSS_ACCESS_KEY, OSS_ENDPOINT);
        $bucket = OSS_BUCKET_XQ;

        $img = str_replace('data:image/png;base64,', '', $img);
        $img = str_replace(' ', '+', $img);
        $content = base64_decode($img);

        $options = array(
            'content' => $content,
            'length' => strlen($content),
            \ALIOSS::OSS_HEADERS => array(
                'Content-Type' => 'image/pjpeg')
        );
        $res = $oss->upload_file_by_content($bucket, 'images/' . $file_name, $options);
        //$msg = "上传本地到 /" . $bucket . "/" . $file_name;
        //OSSUtil::print_res($res, $msg);
        if ($res->isOk()) {
            return  'images/' . $file_name;
        }
    }
}